/*
* @Author: xfy
* @Date:   2017-09-07 10:25:00
* @Last Modified by:   xfy
* @Last Modified time: 2017-09-12 18:53:57
*/
require('./index.css');
require('page/common/admin-nav-top/index.js');
var _supper         = require('util/supper.js');
var _adminProduct   = require('service/admin-product-service.js');
var Pagination      = require('util/pagination/index.js');
var templateIndex   = require('./index.string');

//page逻辑部分
var page = {
    data : {
        listParam : {
            pageNum     : 1,
            pageSize    : 5
        }
    },
    init : function(){
        this.bindEvent();
        this.onLoad();
    },
    onLoad : function(){
        //加载商品信息
        this.loadProductList();
    },
    bindEvent : function(){
        var _this           = this,
            orderListHtml   = '';
        //绑定商品查询
        $(document).on('change', '.form-control', function(){
            if($(this).val() === 'productId'){
                $('#productSearch').attr('placeholder','请输入商品id');
            }else{
                $('#productSearch').attr('placeholder','请输入商品名称');
            }
        });
        //提交搜索结果
        $(document).on('click', '.admin-product-input', function(){
            //判断输入的是否为ID
            if($('.form-control').val() === 'productId'){
                var id = $('#productSearch').val();
                //输入的必需是数字
                if(_this.validateId(id)){
                    _supper.errorTips('输入的不是数字');
                    return;
                }
                _this.productSearch({
                    pageNum     : _this.data.listParam.pageNum,
                    pageSize    : _this.data.listParam.pageSize,
                    id          : $('#productSearch').val()
                });
            }
            //判断输入的是否为名称
            else if($('.form-control').val() === 'productName'){
                _this.productSearch({
                    pageNum     : _this.data.listParam.pageNum,
                    pageSize    : _this.data.listParam.pageSize,
                    name        : $('#productSearch').val()
                });
            }
        });
        //下架商品按钮
        $(document).on('click', '.cell-stop', function(e){
            var $target = $(e.target);
            var productInfo = {
                id : $target.data('productId'),
                status : 0
            };
            _adminProduct.setSaleStatus(productInfo, function(res){
                _supper.successTips(res);
                _this.loadProductList();
            }, function(errMsg){
                _sipper.errorTips(errMsg);
            });
        });
        //上架商品按钮
        $(document).on('click', '.cell-start', function(e){
            var $target = $(e.target);
            var productInfo = {
                id : $target.data('productId'),
                status : 1
            };
            _adminProduct.setSaleStatus(productInfo, function(res){
                _supper.successTips(res);
                _this.loadProductList();
            }, function(errMsg){
                _sipper.errorTips(errMsg);
            });
        });
    },
    //验证搜索id时，是否为数字
    validateId : function(data){
        return !$.isNumeric(data);
    },
    //通过id或名字查找商品
    productSearch : function(productData){
        var _this       = this,
            $listCon    = $('.admin-page-wrap .product-con'),
            formData    = $('.form-control').val();
        _adminProduct.productSearch(productData, function(res){
            //渲染html
            orderListHtml = _supper.renderHtml(templateIndex, res);
            $listCon.html(orderListHtml);
            //回填查询和搜索框
            $('.form-control').val(formData);
            $('#productSearch').val(productData.name || productData.id);
            _this.loadPagination({
                hasPreviousPage : res.hasPreviousPage,
                prePage         : res.prePage,
                hasNextPage     : res.hasNextPage,
                nextPage        : res.nextPage,
                pageNum         : res.pageNum,
                pages           : res.pages
            });
        }, function(errMsg){
            $listCon.html('<div class="error-tip"><p class="error-msg"><i class="fa fa-coffee"></i> 好像出错了哦…</p></div>');
        });
    },
    //加载所有商品
    loadProductList : function(){
        var _this           = this,
            orderListHtml   = '',
            $listCon        = $('.admin-page-wrap .product-con');
        $listCon.html('<div class="loading"></div>');
        _adminProduct.getProductList(this.data.listParam, function(res){
            //渲染html
            orderListHtml = _supper.renderHtml(templateIndex, res);
            $listCon.html(orderListHtml);
            _this.loadPagination({
                hasPreviousPage : res.hasPreviousPage,
                prePage         : res.prePage,
                hasNextPage     : res.hasNextPage,
                nextPage        : res.nextPage,
                pageNum         : res.pageNum,
                pages           : res.pages
            });
        }, function(errMsg){
            $listCon.html('<div class="error-tip"><p class="error-msg"><i class="fa fa-frown-o"></i> 好像出错了哦…</p></div>');
        });

    },
    //加载分页信息
    loadPagination : function(pageInfo){
        var _this = this;
        this.pagination ? '' : (this.pagination = new Pagination());
        this.pagination.render($.extend({}, pageInfo, {
            container : $('.pagination'),
            onSelectPage : function(pageNum){
                _this.data.listParam.pageNum = pageNum;
                _this.loadProductList();
            }
        }));
    }
};
$(function(){
    page.init();
});